package org.jasig.cas.client.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jasig.cas.client.validation.Assertion;

/**
 * 用于项目继承，将CAS Server的用户转换为项目所需的用户对象
 * 
 * @author CreGu
 *
 */
public abstract class AbstractUserAdapterFilter implements Filter {

	public void init(FilterConfig arg0) throws ServletException {

	}

	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		final HttpServletRequest request = (HttpServletRequest) servletRequest;
		final HttpServletResponse response = (HttpServletResponse) servletResponse;

		final HttpSession session = request.getSession(false);
		final Assertion assertion = session != null ? (Assertion) session
				.getAttribute("_const_cas_assertion_") : null;

		if (assertion != null)
			adapteUser(request, response, session, assertion);

		filterChain.doFilter(servletRequest, servletResponse);
	}

	protected abstract void adapteUser(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Assertion assertion);

	public void destroy() {

	}

}
